<script type="text/javascript">
    var tenantCode="";
    var keyId = (getUrlParam("keyId", url));
    $(function () {


        $("#btnSave").click(function () {
            if (!checkForm(document.getElementById("saveForm"))) {
                return;
            }
            $("#supplierPropertys").html("");

            jQuery("input[name='k1']").each(function(index){
                var k = $("input[name='k1']:eq("+index+")").val();
                var v = $("input[name='v1']:eq("+index+")").val();
                var supplierNo = $("#supplierNo").val();

                if (k=="loginPassword" && v.length > 0) {
                    //1及时通  密码需要加密  MD5 32位 转大写
                    //2EMS  密码需要加密MD5 32位 转小写
                    //3微网通联   密码不需要加密MD5
                    if('1'==supplierNo){
                        v=$.md5(v).toUpperCase();
                    }else if('2'==supplierNo){
                        v=$.md5(v).toLowerCase();
                    }
                }
                var supplierPropertyNameHidden = "<input type='hidden' name='propertyLst["+index+"].propertyName' value='"+k+"'/>";
                var supplierPropertyValueHidden = "<input type='hidden' name='propertyLst["+index+"].propertyValue' value='"+v+"'/>";

                $("#supplierPropertys").append(supplierPropertyNameHidden);
                $("#supplierPropertys").append(supplierPropertyValueHidden);

            });

            $("#btnSave").blur();
            showLoading();
            fy_api.ajaxSubmit($("#saveForm"),{
                success: function (data) {
                    hideLoading();
                    if (!data.success) {
                        alert(data.message);
                    } else {
                        alert("保存成功！");
                        parent.grid_get("grid").datagrid("load");
                        turnBack();
                    }
                }
            });
        });
        if(keyId != ""){
            //编辑
            fy_api.load_template({
                controller: $("#saveForm").attr("templateParam", "keyId=" + keyId), loading: true, initValue: true, loaded: loadedEdit
            });
        }else{
            //添加
            $("#divEdit").remove();
            fy_api.load_template({
                controller: $("#saveForm").attr("action","supplier/save.do"), data: {}, loaded: loadedAdd
            });
        }
    });

    function tenantCodeSet(){

        fy_api.ajax({
            url: "smsSetting/getTenantCodeForSelectBox.do?_t=" + new Date().getTime(),
            type: "POST",
            data: "",
            success: function (data) {
                if (data.success) {
                    $("#tenantCodeForSelect").val(data.message);
                    var options = $("#tenantCodeForSelect").val().split(",");
                    for(var i=0;i<options.length;i++){
                        if(options[i] == $("#tenantCodeHid").val()){
                            $("#tenantCode").append("<option selected value="+options[i]+">"+options[i]+"</option>");
                        }else{
                            $("#tenantCode").append("<option value="+options[i]+">"+options[i]+"</option>");
                        }
                    }
                }
            }
        });

    }

    function loadedAdd(){
        createNeedProperty($("#supplierNo").val());
        changeSupplier($("#supplierNo").val());
        tenantCodeSet();
    }

    function loadedEdit(){
        $("#supplierProperty tbody").html("");
        var propertys = $("#propertyLstItems").val();
        var jsonObjs = JSON.parse(propertys);
        for(var i=0;i<jsonObjs.rows.length;i++){
            var propertyItem = jsonObjs.rows[i];
            var propertyName = propertyItem.propertyName;
            var propertyValue = propertyItem.propertyValue;

            if(propertyName == "loginName"){
                $("#supplierProperty>tbody").append('<tr><td align="center">短信平台登录名</td><td align="center"><input type="text" title="属性名" name="k1" value="loginName" readonly="true" /></td><td><input type="text" title="属性值" name="v1" value="'+propertyValue+'"/></td><td></td></tr>');
            }else if(propertyName == "loginPassword"){
                $("#supplierProperty>tbody").append('<tr><td align="center">短信平台密码</td><td align="center"><input type="text" title="属性名" name="k1" value="loginPassword" readonly="true" /></td><td><input type="text" title="属性值" name="v1"/></td><td></td></tr>');
            }else if(propertyName == "timeExpire"){
                $("#supplierProperty>tbody").append('<tr><td align="center">发送短信失效时间</td><td align="center"><input type="text" title="属性名" name="k1" value="timeExpire" readonly="true" /></td><td><input type="text" title="属性值" name="v1" value="'+propertyValue+'"/>秒(默认24小时)</td><td></td></tr>');
            }else if(propertyName == "unitPrice"){
                $("#supplierProperty>tbody").append('<tr><td align="center">发送短信单价</td><td align="center"><input type="text" title="属性名" name="k1" value="unitPrice" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="'+propertyValue+'"/></td><td></td></tr>');
            }else if(propertyName == "voiceUnitPrice"){
                $("#supplierProperty>tbody").append('<tr><td align="center">发送语音短信单价</td><td align="center"><input type="text" title="属性名" name="k1" value="voiceUnitPrice" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="'+propertyValue+'"/></td><td></td></tr>');
            }else if(propertyName == "productid"){
                $("#supplierProperty>tbody").append('<tr><td align="center">产品id</td><td align="center"><input type="text" title="属性名" name="k1" value="productid" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="'+propertyValue+'"/></td><td></td></tr>');
            }else if(propertyName == "autograph"){
                $("#supplierProperty>tbody").append('<tr><td align="center">发送短信签名</td><td align="center"><input type="text" title="属性名" name="k1" value="autograph" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="'+propertyValue+'"/></td><td></td></tr>');
            }else if(propertyName.length>0){
                $("#supplierProperty>tbody").append('<tr style="height: auto;"><td align="center">自定义属性</td><td align="center"><input type="text" title="属性名" name="k1" value="'+propertyName+'"/></td><td align="center"><input type="text" title="属性值" name="v1" value="'+propertyValue+'"/></td><td align="center"><input type="button" onclick="javascript:deleteRow(this);" value="删除" style="width:40px;height:30px"/></td></tr>');
            }
        }
        tenantCodeSet();
        setDisable();
    }

    function setDisable(){
        $("#tenantCode").attr("disabled",true);
        $("#supplierNo").attr("disabled",true);
        //$("#appPlatform").attr("disabled",true);
    }

    function changeSupplier(supplierNo){
        if('1'==supplierNo){
            $("#supplierChannel").val(supplierNo);
            $("#supplierName").val("即时通");
            $("#supplierFullName").val("即时通短信");
        }else if('2'==supplierNo){
            $("#supplierChannel").val(supplierNo);
            $("#supplierName").val("EMS");
            $("#supplierFullName").val("EMS短信");
        }else if('3'==supplierNo){
            $("#supplierChannel").val(supplierNo);
            $("#supplierName").val("微网通联");
            $("#supplierFullName").val("微网通联短信");
        }else if('5'==supplierNo){
            $("#supplierChannel").val(supplierNo);
            $("#supplierName").val("上海助通");
            $("#supplierFullName").val("上海助通信息科技短信");
        }
        createNeedProperty(supplierNo)
    }

    function createNeedProperty(supplierNo){
        $("#supplierProperty tbody").html("");

        $("#supplierProperty>tbody").append('<tr><td align="center">短信平台登录名</td><td align="center"><input type="text" title="属性名" name="k1" value="loginName" readonly="true" /></td><td><input type="text" title="属性值" name="v1" /></td><td></td></tr>');
        $("#supplierProperty>tbody").append('<tr><td align="center">短信平台密码</td><td align="center"><input type="text" title="属性名" name="k1" value="loginPassword" readonly="true" /></td><td><input type="text" title="属性值" name="v1" /></td><td></td></tr>');

        if("5"!=supplierNo){
            $("#supplierProperty>tbody").append('<tr><td align="center">发送短信失效时间</td><td align="center"><input type="text" title="属性名" name="k1" value="timeExpire" readonly="true" /></td><td><input type="text" title="属性值" name="v1" value="86400"/>秒(默认24小时)</td><td></td></tr>');
        }
        $("#supplierProperty>tbody").append('<tr><td align="center">发送短信单价</td><td align="center"><input type="text" title="属性名" name="k1" value="unitPrice" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="0.1"/></td><td></td></tr>');
        if('1'==supplierNo){
            $("#supplierProperty>tbody").append('<tr><td align="center">发送语音短信单价</td><td align="center"><input type="text" title="属性名" name="k1" value="voiceUnitPrice" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="1.8"/></td><td></td></tr>');
        }else if('2'==supplierNo){

        }else if('3'==supplierNo){

        }else if('5'==supplierNo){
            $("#supplierProperty>tbody").append('<tr><td align="center">产品id</td><td align="center"><input type="text" title="属性名" name="k1" value="productid" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="170215"/></td><td></td></tr>');
        }
        $("#supplierProperty>tbody").append('<tr><td align="center">发送短信签名</td><td align="center"><input type="text" title="属性名" name="k1" value="autograph" readonly="true" /></td><td><input type="text" name="v1" title="属性值" value="【】"/></td><td></td></tr>');
    }

    function addProperty(){
        $("#supplierProperty>tbody").append('<tr style="height: auto;"><td align="center">自定义属性</td><td align="center"><input type="text" title="属性名" name="k1" /></td><td align="center"><input type="text" title="属性值" name="v1" /></td><td align="center"><input type="button" onclick="javascript:deleteRow(this);" value="删除" style="width:40px;height:30px"/></td></tr>');
    }
    function deleteRow(obj){
        $(obj).parent().parent().remove();
    }
    function turnBack() {
        parent.j_closeWin();
    }
</script>
<form action="supplier/update.do" method="post" id="saveForm" templateUrl="supplier/get.do">
    <div id="divEdit">
        <input type="hidden" name="id" value="${id}"/>
        <input type="hidden"  name="supplierNo" value="${supplierNo}"/>
        <input type="hidden"  id="tenantCodeHid" name="tenantCode" value="${tenantCode}"/>
        <input type="hidden"  id="propertyLstItems" name="propertyLstItems" value="${remark}"/>
    </div>
    <div>
        <input type="hidden" id="tenantCodeForSelect" name="tenantCodeForSelect"/>
        <input type="hidden" id="tenantHeaderCode" name="tenantHeaderCode" value="${tenantHeaderCode}"/>
        <input type="hidden" id="supplierChannel" name="supplierChannel" value="${supplierChannel}"/>
        <input type="hidden" id="supplierName" name="supplierName" value="${supplierName}"/>
        <input type="hidden" id="supplierFullName" name="supplierFullName" value="${supplierFullName}"/>
    </div>
    <div id="supplierPropertys">
    </div>
    <div class="div-item" style="width: 100%;">
        <div class="div-label"><strong>基本信息设置</strong></div>
    </div>
    <div class="div-item">
        <div class="div-label"><span class="nullTip">*</span>租户唯一标识：</div>
        <select name="tenantCode" id="tenantCode" value="${tenantCode}">

        </select>
    </div>
    <div class="div-item">
        <div class="div-label"><span class="nullTip">*</span>供应商：</div>
        <select name="supplierNo" id="supplierNo" value="${supplierNo}" onchange="changeSupplier(this.value)">
            <option value="1">即时通</option>
            <option value="2">EMS</option>
            <option value="3">微网通联</option>
            <option value="5">上海助通</option>
        </select>
    </div>

    <div class="div-item">
        <div class="div-label"><span class="nullTip">*</span>应用平台：</div>
        <select name="appPlatform" id="appPlatform" value="${appPlatform}">
            <option value="BS">BS</option>
            <option value="CS">CS</option>
            <option value="仓配">仓配</option>
        </select>
    </div>
    <div class="div-item">
        <div class="div-label"><span class="nullTip">*</span>是否启用：</div>
        <select name="isUsed" id="isUsed" value="${isUsed}">
            <option value="Y">启用</option>
            <option value="N">不启用</option>
        </select>
    </div>

    <div class="div-item">
        <div class="div-label"><span class="nullTip">*</span>展示排序顺序：</div>
        <select name="viewSort" id="viewSort" value="${viewSort}">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
        </select>
    </div>
    <div class="div-item" style="width: 100%;">
        <hr>
    </div>
    <div class="div-item" style="width: 100%;">
        <div class="div-label"><strong>供应商属性设置</strong></div>
    </div>

    <div class="div-item" style="width: 100%;height: auto">
        <table id="supplierProperty" border="1" width="80%">
            <thead>
            <tr style="height: 45px">
                <th width="100px">属性描述</th>
                <th width="100px">属性名</th>
                <th width="100px">属性值</th>
                <th width="50px"><input type="button" onclick="addProperty();" value="增加属性" style="width:80px;height:30px"/></th>
            </tr>
            </thead>
            <tbody>


            </tbody>
        </table>
    </div>
</form>
<div class="form-buttons">
    <a id="btnSave" class="easyui-linkbutton" iconCls="icon-save">保存</a>
    <a id="btnBack" class="easyui-linkbutton c2" iconCls="icon-undo" onClick="turnBack();">返回</a>
</div>